package com.jyun;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class LoginFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        String authCoder=servletRequest.getParameter("authCode");
        String authCodes=(String)((HttpServletRequest)servletRequest).getSession().getAttribute("authCode");
        PrintWriter out=new PrintWriter(servletResponse.getOutputStream());

        if(authCoder!=null&&authCodes!=null&&authCoder.equals(authCodes)) {
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else{
            out.println("auth code is error!");
            ((HttpServletResponse)servletResponse).sendRedirect("/login.jsp");
        }
        out.flush();
        out.close();
    }

    public void destroy() {

    }
}
